<?
require_once('functions.php');

function GetAddresses()
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = "SELECT * from Addresses";
    return $results = mysql_query($sql,$con);
    
}
function BlankAddress()
{
    return array('id'=>null,'address'=>null, 'created_at'=>null,'updated_at'=>null,'city'=>null,'state'=>null,'zip_code'=> null);
}
function GetAddress($id)
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = "SELECT * FROM Addresses WHERE id=$id";
    return $results = mysql_query($sql, $con);
}
function DeleteAddress($id)
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = mysql_query("DELETE FROM Addresses WHERE id=$id");
    return $results = mysql_query($sql, $con);
}
function SaveAddress(&$row)
{
    $con = GetConnection();
	$db = mysql_select_db("n02447784_db",$con);
    if(empty($row['id']))
    {
        $sql =  "INSERT INTO Addresses(address, city,state,zip_code) 
               VALUES ('$row[address]', '$row[city]', '$row[state]', '$row[zip_code]' ) ";
        }else{
        $sql =  "UPDATE Addresses 
               SET address = '$row[address]', updated_at = NOW(), city = '$row[city]', state = '$row[state]', zip_code = '$row[zip_code]' 
               WHERE id=$row[id] ";
    }
    	$return = mysql_query($sql,$con);
				
				mysql_close($con);
				return $return;
}
function ValidateAddress($row)
{
    $errors = array();
	if(empty($row['address'])) $errors['address'] = 'Address is required';
    if(empty($row['city'])) $errors['city'] = 'City is required';
    if(empty($row['state'])) $errors['state'] = 'State is required';
	if(empty($row['zip_code'])) $errors['zip_code'] = 'Zip Code is required';   
    return count($errors) == 0 ? true : $errors;
}
?>